package utils;

import java.util.Locale;
import javax.naming.*;
import javax.sql.DataSource;

public final class DatabaseConnection {
    private static volatile DatabaseConnection instance;
    private static DataSource ds;

    public static DatabaseConnection getInstance(String connectionString) {
        if (instance == null) {
            synchronized (DatabaseConnection.class) {
                if (instance == null) {
                    instance = new DatabaseConnection(connectionString);
                }
            }
        }
        return instance;
    }

    public DataSource getDataSource() {
        return ds;
    }

    private DatabaseConnection(String connectionString) {
        try {
            Locale.setDefault(Locale.ENGLISH);
            Context c = new InitialContext();
            Object ods = c.lookup(connectionString);
            ds = (DataSource) ods;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
